package com.example.graduationdesignbackend.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.example.graduationdesignbackend.constants.ResponseCode;
import com.example.graduationdesignbackend.dto.AdminLoginDto;
import com.example.graduationdesignbackend.entity.Admin;
import com.example.graduationdesignbackend.exception.ParkException;
import com.example.graduationdesignbackend.mapper.AdminMapper;
import com.example.graduationdesignbackend.res.Res;
import com.example.graduationdesignbackend.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuze
 * @since 2024-05-08
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public SaTokenInfo login(AdminLoginDto adminLoginDto) {
        String code = adminLoginDto.getCode();
        String phone = adminLoginDto.getPhone();
        Object redis_code = redisTemplate.opsForValue().get("check:code:" + phone);
        if(redis_code == null ) throw new ParkException(ResponseCode.Fail, "验证码错误");
        if ( !redis_code.equals(code)) throw new ParkException(ResponseCode.Fail, "验证码错误");
        //1.查询用户是否存在
        Admin admin = lambdaQuery().eq(Admin::getPhone,adminLoginDto.getPhone()).one();
        if (BeanUtil.isEmpty(admin)){
            admin = new Admin();
            admin.setId(RandomUtil.randomNumbers(9));
            admin.setStatus(9);
            admin.setPhone(adminLoginDto.getPhone());
            admin.setCreateTime(new Date().getTime());
            admin.setPower("super-admin");
            Boolean save = save(admin);
            if (!save) throw  new ParkException(ResponseCode.Fail);
        }else {
            admin.setUpdateTime(new Date().getTime());
            Boolean update = updateById(admin);
            if (!update) throw new ParkException(ResponseCode.Fail);
        }
        Admin admin1 = lambdaQuery().eq(Admin::getPhone,adminLoginDto.getPhone()).one();
        StpUtil.login(admin1.getId());
        return StpUtil.getTokenInfo();
    }

    @Override
    public SaTokenInfo loginpwd(AdminLoginDto adminLoginDto) {
        Admin admin = lambdaQuery().eq(Admin::getPhone,adminLoginDto.getPhone()).one();
        if (BeanUtil.isEmpty(admin)) throw new ParkException(ResponseCode.NotFindError);
        if (!admin.getPassword().equals(adminLoginDto.getPassword()))throw new ParkException(ResponseCode.Fail,"手机号或密码错误！");
        admin.setUpdateTime(new Date().getTime());
        Boolean update = updateById(admin);
        if (!update) throw  new ParkException(ResponseCode.Fail);
        StpUtil.login(admin.getId());
        return StpUtil.getTokenInfo();
    }

    @Override
    public Res editpwd(Admin admin1) {
        String id = StpUtil.getLoginIdAsString();
        Admin admin = lambdaQuery().eq(Admin::getId,id).one();
        admin.setPassword(admin1.getPassword());
        Boolean update = updateById(admin);
        if (!update) throw new ParkException(ResponseCode.Fail);
        return Res.yuze();
    }

    @Override
    public Admin getInfo(String id) {
        return getById(id);
    }
}
